﻿using System;
using System.Text;
using System.IO;

namespace dbapi_lib.API.Util
{
    /// <summary>
    /// Creates log file.
    /// </summary>
    class ErrorLogger
    {
        /// <summary>
        /// Create or open directory and log file.
        /// </summary>
        /// <param name="text">The string to be written into the log file.</param>
        private static void makeLog(String text)
        {
            String dirPath = "Log";
            String filePath = @"Log\log.txt";

            if (!Directory.Exists(dirPath))
            {
                DirectoryInfo dir = Directory.CreateDirectory(dirPath);
            }   

            using (StreamWriter sw = File.AppendText(filePath))
            {
                sw.WriteLine(text);
            }    
        }

        /// <summary>
        /// Logging info.
        /// </summary>
        /// <param name="text">Describes the info.</param>
        public static void logInfo(String text)
        {          
            StringBuilder sb = new StringBuilder();

            sb.Append("\r\nLog Entry :  ");
            sb.Append(DateTime.Now.ToLongTimeString()+"   ");
            sb.Append(DateTime.Now.ToLongDateString());
            sb.AppendLine();
            sb.Append("[INFO]");
            sb.AppendLine();
            sb.Append("Description: "+text);
            sb.AppendLine();
            sb.Append("---------------------------------");
            sb.AppendLine();
            sb.Append("---------------------------------");
            sb.AppendLine();

            makeLog(sb.ToString());
        }

        /// <summary>
        /// Logging warning.
        /// </summary>
        /// <param name="exc">The occured exception.</param>
        /// <param name="text">Note for the warning.</param>
        public static void logWarn(Exception exc, String text = "")
        {
            StringBuilder sb = new StringBuilder();

            sb.Append("\r\nLog Entry :  ");
            sb.Append(DateTime.Now.ToLongTimeString() + "   ");
            sb.Append(DateTime.Now.ToLongDateString());
            sb.AppendLine();
            sb.Append("[WARNING]");
            sb.AppendLine();
            sb.Append("Exception: " + exc.GetType());
            sb.AppendLine();
            sb.Append("Description: " + exc.Message);
            sb.AppendLine();
            sb.Append("Location: " + exc.Source);
            sb.AppendLine();
            sb.Append("Function: " + exc.TargetSite);
            sb.AppendLine();
            sb.Append("Message: " + text);
            sb.AppendLine();
            sb.Append("---------------------------------");
            sb.AppendLine();
            sb.Append("---------------------------------");
            sb.AppendLine();

            makeLog(sb.ToString());
        }

        /// <summary>
        /// Logging error.
        /// </summary>
        /// <param name="exc">The occured exception.</param>
        /// <param name="text">Note for the error.</param>
        public static void logError(Exception exc, String text = "")
        {
            StringBuilder sb = new StringBuilder();

            sb.Append("\r\nLog Entry :  ");
            sb.Append(DateTime.Now.ToLongTimeString() + "   ");
            sb.Append(DateTime.Now.ToLongDateString());
            sb.AppendLine();
            sb.Append("[ERROR]");
            sb.AppendLine();
            sb.Append("Exception: " + exc.GetType());
            sb.AppendLine();
            sb.Append("Description: " + exc.Message);
            sb.AppendLine();
            sb.Append("Location: " + exc.Source);
            sb.AppendLine();
            sb.Append("Function: " + exc.TargetSite);
            sb.AppendLine();
            sb.Append("Message: " + text);
            sb.AppendLine();
            sb.Append("---------------------------------");
            sb.AppendLine();
            sb.Append("---------------------------------");
            sb.AppendLine();

            makeLog(sb.ToString());
        }
    }
}
